package com.edge.pbx.view.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.edge.pbx.view.home.PbxMainMenuRootLayout.FIX_HEIGHT;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 *
 * 934 x 1440
 *
 *  60 50 200 200
 *
 */
public class PbxMainMenuLeftLayout extends ViewGroup {

    public PbxMainMenuLeftLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private float mCalScale = 1f;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
//        YzDebug.d("PBX","onMeasure " + width +" " + height +" " + getChildCount());
        mCalScale = (float) height/FIX_HEIGHT;
        measureChild(getChildAt(0),200,200,height);
        int tw = 220;
        int th = 198;
        measureChild(getChildAt(1),tw,th,height);
        measureChild(getChildAt(2),tw,220,height);
        measureChild(getChildAt(3),tw,220,height);
        measureChild(getChildAt(4),tw,th,height);
        setMeasuredDimension(width, height);
    }

    private void measureChild(View child,int childFixW,int childFixH, int parentHeight){
//        YzDebug.d("PBX","measureChild:" + child +"    sss:"+childFixW +" " + childFixH);
        if (child == null){

            return;
        }
        int height = (int) (childFixH * mCalScale);
        int width  = (int) (childFixW * mCalScale);
//        YzDebug.d("PBX","measureChild: " + child + " ->"+ width+" " + height);
        child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        layoutChild(getChildAt(0),60, 50);
        layoutChild(getChildAt(1),276, 275);
        layoutChild(getChildAt(2),360, 574);
        layoutChild(getChildAt(3),302, 868);
        layoutChild(getChildAt(4),134, 1160);
    }
    private void layoutChild(View child, int left, int top){
        if (child == null)
            return;
        final int l = (int) (left *mCalScale);
        final int t = (int) (top * mCalScale);
//        YzDebug.d("PBX","layoutChild:"+ l +" " + t  +" " +(l+child.getMeasuredWidth())
//        +" " + (t+child.getMeasuredHeight()));
        child.layout(l, t, l+ child.getMeasuredWidth(), t+ child.getMeasuredHeight());
    }
}
